package com.amigo.online.common.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.amigo.online.common.core.response.Response;
import com.google.gson.Gson;
import javafx.scene.input.DataFormat;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Configuration
public class UploadTempUtil implements CommandLineRunner {
	public static String url = "/data";
	public static String token = "tevitalk";
	@Value("${amigo.uploadUrl:#{null}}")
	private String uploadUrl;

	static SimpleDateFormat formatter = new SimpleDateFormat("yyyy/mm/dd/");
	@Override
	public void run(String... args) throws Exception {
		if(uploadUrl!=null){
			url = uploadUrl;
		}
	}
//	public static String uploadResource(MultipartFile[] files, String token)
//			throws ClientProtocolException, IOException {
//		CloseableHttpClient client = HttpClients.createDefault();
////		String url = "http://128.14.178.186:80/upload";
//
//		HttpPost upload = new HttpPost(url);
//		upload.addHeader("ACCESS_TOKEN", UploadTempUtil.token);
//		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//		for (MultipartFile multipartFile : files) {
//			builder.addBinaryBody("file", multipartFile.getBytes(), ContentType.APPLICATION_OCTET_STREAM,
//					multipartFile.getOriginalFilename());
//		}
//		HttpEntity entity = builder.build();
//		upload.setEntity(entity);
//		CloseableHttpResponse response = client.execute(upload);
//		HttpEntity entity2 = response.getEntity();
//		String result = EntityUtils.toString(entity2, "UTF-8");
//		System.out.println(result);
////		Response parseObject = new Gson().fromJson(result, Response.class);
//		return result;
//	}

	//	public static String uploadResourceVideo(MultipartFile file, String token)
//			throws ClientProtocolException, IOException {
//		CloseableHttpClient client = HttpClients.createDefault();
//
////		String url = "http://10.12.104.14:81/upload";
//		HttpPost upload = new HttpPost(url);
//		upload.addHeader("ACCESS_TOKEN", UploadTempUtil.token);
//		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//		builder.addBinaryBody("file", file.getBytes(), ContentType.APPLICATION_OCTET_STREAM,
//				file.getOriginalFilename());
//		HttpEntity entity = builder.build();
//		upload.setEntity(entity);
//		CloseableHttpResponse response = client.execute(upload);
//		HttpEntity entity2 = response.getEntity();
//		String result = EntityUtils.toString(entity2, "UTF-8");
//		System.out.println(result);
////		Response parseObject = new Gson().fromJson(result, Response.class);
//
//		return result;
//	}
	public static String uploadResourceVideo(MultipartFile file, String token)
			throws IOException {
		return uploadFilePassjson(file);
	}


	public static String uploadFile(MultipartFile multipartFile) throws IOException {
		String path = formatter.format(new Date());
		//创建目录
		File filePath = new File(url + path);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		String uuidpath = path + UUID.randomUUID().toString();
		//写入本地
		File localFile = new File(url + uuidpath);
		multipartFile.transferTo(localFile);

		return uuidpath;
	}

	public static String uploadFilePassjson(MultipartFile multipartFile) throws IOException {
		return passjson(uploadFile(multipartFile));
	}
	private static String passjson(String uuidpath){
		String body ="{\n" +
				"    \"data\": [\n" +
				"        \""+ uuidpath +"\"\n" +
				"    ],\n" +
				"    \"meta\": {\n" +
				"        \"msg\": \"请求成功\",\n" +
				"        \"code\": 200,\n" +
				"        \"success\": true\n" +
				"    }\n" +
				"}";

		System.out.println(body);
		return body;
	}


	//	public static String uploadResourceImg(MultipartFile file, String token)
//			throws ClientProtocolException, IOException {
//		CloseableHttpClient client = HttpClients.createDefault();
//
//		HttpPost upload = new HttpPost(url);
//		upload.addHeader("ACCESS_TOKEN", UploadTempUtil.token);
//		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//		builder.addBinaryBody("file", file.getBytes(), ContentType.APPLICATION_OCTET_STREAM,
//				file.getOriginalFilename());
//		HttpEntity entity = builder.build();
//		upload.setEntity(entity);
//		CloseableHttpResponse response = client.execute(upload);
//		HttpEntity entity2 = response.getEntity();
//		String result = EntityUtils.toString(entity2, "UTF-8");
//		System.out.println("url:  " + url + "\n" + result);
////		Response parseObject = new Gson().fromJson(result, Response.class);
//		return result;
//	}
	public static String uploadResourceImg(MultipartFile file, String token){
		try {
			return uploadFilePassjson(file);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("文件上传失败");
		}
	}



	public static String uploadImg(MultipartFile file, String token)
			throws ClientProtocolException, IOException {
		return uploadFile(file);
	}
	//
//	public static String uploadImg(MultipartFile file, String token)
//			throws ClientProtocolException, IOException {
//		CloseableHttpClient client = HttpClients.createDefault();
//
//		HttpPost upload = new HttpPost(url);
//		upload.addHeader("ACCESS_TOKEN", UploadTempUtil.token);
//		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//		builder.addBinaryBody("file", file.getBytes(), ContentType.APPLICATION_OCTET_STREAM,
//				file.getOriginalFilename());
//		HttpEntity entity = builder.build();
//		upload.setEntity(entity);
//		CloseableHttpResponse response = client.execute(upload);
//		HttpEntity entity2 = response.getEntity();
//		String result = EntityUtils.toString(entity2, "UTF-8");
//		System.out.println("url:  " + url + "\n" + result);
////		Response parseObject = new Gson().fromJson(result, Response.class);
//		return getAddress(result);
//	}
	public static  String getAddress(String result) {
		String address = "";
		Response parseObject = new Gson().fromJson(result, Response.class);
		if (parseObject != null && parseObject.getData() != null) {
			Object data = parseObject.getData();
			if (data instanceof List) {
				List<String> addressList = (List<String>) data;
				address = addressList.get(0);
			}
		}
		return address;
	}


}
